# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能，如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

# import xbot
# from xbot import print, sleep
# from .import package
import time

from urllib3.exceptions import InsecureRequestWarning


def main(args):
    pass


# coding=utf8
import requests


class RequestsEdit(object):
    headers = {
        # "Accept": "application/json, text/plain, */*",
        # "Accept-Encoding": "gzip, deflate, br",
        "Content-Type": "application/json",
        # "Accept": "application/json, text/javascript, */*; q=0.01",
        # "Accept-Language": "zh-CN,zh;q=0.9",
        "endpointapptype": "promote_app"
        # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
    }

    @classmethod
    def post(cls, url, post_data, headers=headers):
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
        # proxies = {'http': 'http://localhost:8001', 'https': 'http://localhost:8001'}
        success = False
        nums = 0
        s = requests.session()
        s.keep_alive = False  # 关闭多余连接
        while not success:
            try:
                # rep = requests.post(url=url, headers=headers, json=post_data, proxies=proxies, verify=False)
                rep = requests.post(url=url, headers=headers, json=post_data, verify=False)
                success = True
            except:
                time.sleep(2)
                nums += 1
                if nums == 3:
                    rep = None
                    break

        return rep

    @classmethod
    def get(cls, url, headers=headers):
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
        # proxies = {'http': 'http://localhost:8001', 'https': 'http://localhost:8001'}
        success = False
        nums = 0
        s = requests.session()
        s.keep_alive = False  # 关闭多余连接
        while not success:
            try:
                # rep = requests.get(url, headers=headers, proxies=proxies, verify=False)
                rep = requests.get(url, headers=headers, verify=False)
                success = True
            except:
                time.sleep(2)
                nums += 1
                if nums == 3:
                    rep = None
                    break

        return rep

    @classmethod
    def postWithoutProxy(cls, post_data, url):

        success = False
        nums = 0
        s = requests.session()
        s.keep_alive = False  # 关闭多余连接
        while not success:
            try:
                rep = requests.post(url=url, headers=cls.headers, json=post_data)
                success = True
                text = rep.text
            except:
                time.sleep(2)
                nums += 1
                if nums == 3:
                    text = None
                    break

        return text

    @classmethod
    def getWithoutProxy(cls, url, headers=headers):

        success = False
        nums = 0
        s = requests.session()
        s.keep_alive = False  # 关闭多余连接
        while not success:
            try:
                rep = requests.get(url, headers=headers, verify=False)
                success = True
            except:
                time.sleep(2)
                nums += 1
                if nums == 3:
                    rep = None
                    break
        return rep